﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Jemgine.MapEditor.Controls
{
    public partial class ResourceDialog : Form
    {
        ContentManifestExtensions.ContentType _resourceType;

        public ResourceDialog()
        {
            InitializeComponent();
            _resourceType = ContentManifestExtensions.ContentType.Unknown;
        }

        public ResourceDialog(ContentManifestExtensions.ContentType Type)
        {
            InitializeComponent();
            _resourceType = Type;
        }

        private String _selectedResource = "";
        public String SelectedResource
        {
            get { return _selectedResource; }
            private set
            {
                _selectedResource = value;
                resourcePreview.SelectedResource = value;
                Invalidate();
            }
        }

        EditorState _editorState;

        public void Initialize(EditorState _editorState, String _initialResource)
        {
            this._editorState = _editorState;
            resourcePreview.Initialize(_editorState, _resourceType, _initialResource);
            int SelectItem = -1;
        
            foreach (var Item in _editorState.Content.Manifest.Entries)
            {
                if (Item.Type == _resourceType)
                {
                    var Entry = new ListViewItem(Item.Name, "");
                    if (Item.Name == _initialResource) SelectItem = resourceList.Items.Count;
                    resourceList.Items.Add(Entry);
                }
            }

            if (SelectItem != -1)
            {
                resourceList.SelectedIndices.Add(SelectItem);
                resourceList.EnsureVisible(SelectItem);
            }
        }

        private void resourceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (resourceList.SelectedItems.Count <= 0)
                SelectedResource = "";
            else
                SelectedResource = resourceList.SelectedItems[0].Text;
            Invalidate();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
